﻿using System.Collections.Generic;

namespace Foundation.FileAssotiations
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class FileAssociation : ViewModel
    {
        public static string CurrentProgramId = "Poet";

        public FileAssociation(string extension)
        {
            extension = extension.StartsWith(".") ? extension : string.Format(".{0}", extension);
            ExtensionInfo = ExtensionInfo.Get(extension);
            ProgramInfo = ProgramInfo.Get(CurrentProgramId + extension);
            ProgramInfo.Description = "Poet text file";
            ProgramInfo.Verbs = new List<ProgramVerb>
                {new ProgramVerb("open", InstanceManager.ExecutableFilePath + " \"%1\"")};
            ProgramInfo.DefaultIcon = new ProgramIcon {Path = InstanceManager.ExecutableFilePath};
        }

        public bool IsAssotiated
        {
            get { return ProgramInfo.ProgramId == ExtensionInfo.ProgramId; }
            set
            {
                if (value)
                {
                    ExtensionInfo.ProgramId = ProgramInfo.ProgramId;
                    ExtensionInfo.Set(ExtensionInfo);
                    ProgramInfo.Set(ProgramInfo);
                }
                else
                {
                    ExtensionInfo.ProgramId = null;
                    ExtensionInfo.Delete(ExtensionInfo.Extension);
                    ShellNotification.NotifyOfChange();
                }

                RaisePropertyChangedEvent(() => IsAssotiated);
            }
        }

        public ExtensionInfo ExtensionInfo { get; set; }

        public ProgramInfo ProgramInfo { get; set; }
    }
}
